<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="所属供应商" prop="suppliername">
        <el-input
          v-model="queryParams.suppliername"
          placeholder="请输入所属供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="零部件名称" prop="partname">
        <el-input
          v-model="queryParams.partname"
          placeholder="请输入零部件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="进价" prop="primeprice">
        <el-input
          v-model="queryParams.primeprice"
          placeholder="请输入进价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格" prop="specification">
        <el-input
          v-model="queryParams.specification"
          placeholder="请输入规格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="零部件类型" prop="type">
        <el-input
          v-model="queryParams.type"
          placeholder="请输入零部件类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['buypart:buypart:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['buypart:buypart:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['buypart:buypart:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['buypart:buypart:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="buypartList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="零部件ID" align="center" prop="partid" /> -->
      <!-- <el-table-column label="状态" align="center" prop="status" /> -->
      <el-table-column label="所属供应商" align="center" prop="suppliername" />
      <!-- <el-table-column label="所在仓库" align="center" prop="warehouseid" />
      <el-table-column label="客户" align="center" prop="customerid" />
      <el-table-column label="销售员" align="center" prop="salespersonid" /> -->
      <el-table-column label="零部件名称" align="center" prop="partname" />
      <el-table-column label="零部件数量" align="center" prop="quantity" />
      <el-table-column label="零部件类型" align="center" prop="type" />
      <el-table-column label="零部件种类" align="center" prop="modified"/>
      <el-table-column label="规格" align="center" prop="specification" />
      <el-table-column label="进价" align="center" prop="primeprice" />
      <!-- <el-table-column label="售价" align="center" prop="price" /> -->
      <el-table-column label="创建时间" align="center" prop="createtime" />
      <el-table-column label="修改时间" align="center" prop="modifytime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['buypart:buypart:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['buypart:buypart:remove']"
          >删除</el-button> -->
          <el-button
            size="medium"
            type="text"
            icon="el-icon-shopping-cart-2"
            @click="handlecreatebuyorder(scope.row)"
            v-hasPermi="['buypart:buypart:edit']"
          >生成订单</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改购买零部件对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 购买零部件(下订单)对话框 -->
    <el-dialog :title="title" :visible.sync="openorder" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="所属仓库" prop="warehouseid">
                  <el-select v-model="form.warehouseid" placeholder="请输入所属仓库">
                    <el-option
                      v-for="item in warehouseList"
                      :key="item.warehouseid"
                      :label="item.warehousename"
                      :value="item.warehouseid">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="确定售价" prop="price">
                  <el-input v-model="form.price" placeholder="请输入售价" />
                </el-form-item>
                <el-form-item label="联系电话" prop="contactnumber">
                  <el-input v-model="form.contactnumber" placeholder="请输入联系电话" />
                </el-form-item>
                <el-form-item label="仓库地址" prop="address">
                  <el-input v-model="form.address" placeholder="请输入地址" />
                </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBuypart, getBuypart, delBuypart, addBuypart
, updateBuypart } from "@/api/buypart/buypart";
import { listWarehouse } from "@/api/warehouse/warehouse";

export default {
  name: "Buypart",
  data() {
    return {

      warehouseList:[],

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 购买零部件表格数据
      buypartList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openorder:false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierid: null,
        suppliername: null,
        partname: null,
        primeprice: null,
        specification: null,
        type: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        warehouseid: [
          { required: true, message: "仓库不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "售价不能为空", trigger: "blur" }
        ],
        contactnumber: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "地址不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {


    /** 查询购买零部件列表 */
    getList() {
      this.loading = true;
      listBuypart(this.queryParams).then(response => {
        this.buypartList = response.rows;
        this.total = response.total;
        this.loading = false;
      });

      listWarehouse().then(response => {
        this.warehouseList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.openorder = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        partid: null,
        status: null,
        supplierid: null,
        warehouseid: null,
        customerid: null,
        salespersonid: null,
        partname: null,
        primeprice: null,
        price: null,
        specification: null,
        type: null,
        createtime: null,
        modifytime: null,
        deleted: null,
        quantity: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.partid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加购买零部件";
    },
    //下订单按钮操作
    handlecreatebuyorder(row){
      this.reset();
      const partid = row.partid || this.ids
      getBuypart(partid).then(response => {
        this.form = response.data;
        this.openorder = true;
        this.title = "下订单—购买零部件";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const partid = row.partid || this.ids
      getBuypart(partid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改购买零部件";
      });
    },
    /** 提交按钮   提交下的订单*/
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.partid != null) {
            console.log(this.form);
            updateBuypart(this.form).then(response => {
              this.$modal.msgSuccess("生成订单成功");
              this.openorder = false;
              this.getList();
            });

            /* console.log(this.form);
            updatePurchasevehicle(this.form).then(response => {
              this.$modal.msgSuccess("生成订单成功");
              this.open = false;
              this.getList();
            }); */

          } else {
            addBuypart(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openorder = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const partids = row.partid || this.ids;
      this.$modal.confirm('是否确认删除购买零部件编号为"' + partids + '"的数据项？').then(function() {
        return delBuypart(partids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('buypart/buypart/export', {
        ...this.queryParams
      }, `buypart_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
